#!/bin/bash
# WHAT	: addautomator_vz.sh
# WHEN	: 9 JULY, 2007
# WHY	: Add authorized_keys to all VEIDs mentioned on veids file
# WHO	: Mohit Suley, TCS

set -x
source eti_helper.sh


VEID_PATH=/vz/private
AUTH_KEYS=/root/.ssh/authorized_keys

# Grab all VEIDs
alert "Grabbing VEIDs from VZ directory"
ls $VEID_PATH > veids.tmp1
awk '{for(i=1;i<=NF;i++) print $i}' veids.tmp1 > veids.tmp2


alert "Scrubbing veids"
for i in `cat veids.tmp2`; 
do
	echo $i | grep [^0-9] > /dev/null 2>&1 
	
	if [ "$?" -eq "0" ]; then
		echo $i >> veids.discarded
	else 
		echo $i >> veids
	fi	

done


rm -f veids.tmp1 veids.tmp2


alert "Loop to check .ssh and put authorized keys in there"
for veid in `cat veids`; 
do 
	if [! -d ${VEID_PATH}/$veid/root/root/.ssh ]; then
	{
		alert ".ssh didn't exist on $veid"
		mkdir ${VEID_PATH}/$veid/root/root/.ssh
		chmod 600 ${VEID_PATH}/$veid/root/root/.ssh
	}
	fi

	echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAsAPcOmnZeq4UdQDny4Run1WZXdxRO8NVnFCcL2uydPUKAUOa+qUFYkb+xnf3WTlY8LDg+HzMwWF20W0h2RBC4GNSP7pfvbLYqE1Wq6c44M7gWp31FrRsQJ7cmjtZ0sRkJUKa8dF0F3N1rYBciVjJpP6NlaNPCNh40i/cl+hcQu0= automator@cihcispweb220" >> ${VEID_PATH}/$veid/root/root/.ssh/authorized_keys
	alert "Copied to $veid..."

done

